package com.mo.system.permission.service.impl;

import com.mo.global.model.system.Job;
import com.mo.global.model.system.Module;
import com.mo.global.model.system.Role;
import com.mo.global.model.system.Staff;
import com.mo.global.utils.ResultBody;
import com.mo.system.permission.dao.IPermissionDao;
import com.mo.system.permission.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther:Lang
 * @Date:2022-12-08
 * @Time:14:11
 * @Description:TODO
 */
@Service
public class PermissionService implements IPermissionService {
    @Autowired
    private IPermissionDao permissionDao;

    @Override
    public Staff loginCheck(String username, String password) {
        return permissionDao.queryStaffByNameAndPassword(username, password);
    }

    @Override
    public Job queryJobByStaff(Staff staff) {
        return permissionDao.queryJobByStaff(staff);
    }

    @Override
    public Role queryRoleByStaff(Staff staff) {
        return permissionDao.queryRoleByStaff(staff);
    }

    @Override
    public List<List<Module>> queryModuleByStaff(Staff staff) {
        List<Module> modules = permissionDao.queryModuleByStaff(staff);
        List<List<Module>> moduleLists = new ArrayList<>();
        //对查出来的模块进行遍历
        for (Module module : modules) {
            //找出一级模块 id=0 且 state=1
            if (module.getModulePid() == 0 && "1".equals(module.getModuleState())) {
                List<Module> list = new ArrayList<>();
                list.add(module);
                //加入 moduleLists
                moduleLists.add(list);
            }
        }
        for (List<Module> moduleList : moduleLists) {
            Integer moduleId = moduleList.get(0).getModuleId();
            for (Module module : modules) {
                if (module.getModulePid().equals(moduleId) && "1".equals(module.getModuleState())) {
                    moduleList.add(module);
                }
            }
        }
        return moduleLists;
    }

}
